import { defineStore } from "pinia"

export const useSettingStore = defineStore("setting", {
  state: () => {
    return {
      drawer: false,
      layoutMode: localStorage.getItem("layoutMode") || "SingleSidebar", // 布局模式
      theme: localStorage.getItem("theme") || "#409eff", // 系统主题色
      isCollapse: false, // 是否折叠菜单
      isGray: localStorage.getItem("isGray") === "true", // 是否灰色模式
    }
  },
  actions: {
    setDrawer(drawer) {
      this.drawer = drawer
    },
    setLayoutMode(layoutMode) {
      this.layoutMode = layoutMode
      localStorage.setItem("layoutMode", layoutMode)
    },
    setTheme(theme) {
      this.theme = theme
      localStorage.setItem("theme", theme)
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    toggleGray() {
      this.isGray = !this.isGray
      document.body.classList.toggle("gray-mode", this.isGray)
      localStorage.setItem("isGray", this.isGray)
    },
    initIsGray() {
      const grayDays = ["04-04", "12-13"] // 需要变灰的日期
      const today = new Date()
      const todayStr = `${String(today.getMonth() + 1).padStart(
        2,
        "0"
      )}-${String(today.getDate()).padStart(2, "0")}`

      // 如果今天是灰色模式的日期，则开启灰色模式
      if (grayDays.includes(todayStr)) {
        this.isGray = true
        document.body.classList.add("gray-mode")
      } else {
        this.isGray = localStorage.getItem("isGray") === "true"
        if (this.isGray) {
          document.body.classList.add("gray-mode")
        }
      }
    },
  },
})
